2010-09-13 71 views
2

我想讓應用程序給我的SD卡中可用的音頻文件列表,然後我應該能夠從我的應用程序播放該音頻文件。甚至暫停恢復音頻播放等功能..android:使用媒體

對此有何幫助?

回答

1

你可以看到下面的代碼從URL流式音頻

 private void playVideo() { 
     try { 
      final String path = "http://www.a1freesoundeffects.com/animals12557/catmeow.wav";    

      // If the path has not changed, just start the media player 
      if (path.equals(current) && mp != null) { 
       mp.start(); 
       return; 
      } 
      current = path; 

      // Create a new media player and set the listeners 
      mp = new MediaPlayer();    
      mp.setDataSource(path); 
      mp.prepare(); 
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 


     } catch (Exception e) { 
      if (mp != null) { 
       mp.stop(); 
       mp.release(); 
      } 
     } 
    } 
+0

感謝krunal爲你的努力..好的 – 2010-09-14 11:14:51

+0

如果你有任何想法,我如何從我的Android應用程序找到DLNA設備列表?所以我可以將媒體轉移到該設備上 – 2010-09-14 11:15:53

1

你可以試試這種方式。

class Mp3Filter implements FilenameFilter { 
    public boolean accept(File dir, String name) { 
    return (name.endsWith(".mp3")); 
    } 
} 

public class AudioPlayer extends ListActivity implements OnClickListener{ 

private static final String MEDIA_PATH = new String("/sdcard/backup/songs"); 
private List<String> songs = new ArrayList<String>(); 
private MediaPlayer mp = new MediaPlayer(); 
private int currentPosition = 0; 
private static final String TAG = "Audio Player Demo "; 
private static final String isPlaying = "Media is Playing"; 
private static final String notPlaying = "Media has stopped Playing"; 
Button playerButton; 

public void onClick(View v) { 
    if (v.getId() == R.id.play) { 
     playPause(); 
    } 
} 

@Override 
public void onCreate(Bundle icicle) { 
    try { 
     super.onCreate(icicle); 
     setContentView(R.layout.songlist); 
     playerButton = (Button) this.findViewById(R.id.play); 
     playerButton.setText(R.string.stop_label); 
     playerButton.setOnClickListener(this);   
     updateSongList(); 
     //demoPlay(); 
    } catch (NullPointerException e) { 
     Log.v(getString(R.string.app_name), e.getMessage()); 
    } 
} 

public void updateSongList() { 
    File home = new File(MEDIA_PATH); 
    if (home.listFiles(new Mp3Filter()).length > 0) { 
     for (File file : home.listFiles(new Mp3Filter())) { 
      songs.add(file.getName()); 
     }  
     ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,songs); 
     setListAdapter(songList); 
    }  
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) {  
     currentPosition = position; 
     playSong(MEDIA_PATH + songs.get(position)); 
} 


    private void playSong(String songPath) { 
     try { 
     mp.reset(); 
     mp.setDataSource(songPath); 
     mp.prepare(); 
     mp.start(); 
     // Setup listener so next song starts automatically 
     mp.setOnCompletionListener(new OnCompletionListener() { 
     public void onCompletion(MediaPlayer arg0) { 
      nextSong();} 
     }); 
    } catch(IOException e) { 
     Log.v(getString(R.string.app_name), e.getMessage()); 
    } 

    } 

    private void nextSong() { 
    if (++currentPosition >= songs.size()) { 
     // Last song, just reset currentPosition 
     currentPosition = 0; 
    // playSong(MEDIA_PATH + songs.get(currentPosition)); 
    } else { 
    // Play next song 
    playSong(MEDIA_PATH + songs.get(currentPosition)); 
    } 
    } 

private void demoPause(){ 
    mp.pause(); 
    playerButton.setText(R.string.play_label); 
    Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show(); 
    Log.d(TAG, notPlaying); 
} 

// Initiate playing the media player 
private void demoPlay(){ 
    mp.start(); 
    playerButton.setText(R.string.stop_label); 
    Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show(); 
    Log.d(TAG, isPlaying); 
} 

// Toggle between the play and pause 
private void playPause() { 
    if(mp.isPlaying()) { 
     demoPause(); 
    } else { 
     demoPlay(); 
    } 
}  

}

+0

@ Krunal:感謝烏拉圭回合的答案...我想知道,如果我想補充對此,我應該能夠從互聯網流媒體比我怎麼能做到這一點?任何想法???再次感謝您 – 2010-09-14 06:21:35

+0

我在下面添加了答案,您還可以參考此鏈接獲取更多信息http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video .html – 2010-09-14 07:03:04