我打算做一个音乐播放器,用户可以从列表中选择一首歌。歌曲将根据用户的选择进行播放。现在,如果我只播放一首我们可以清楚地将网址放入代码中的歌曲,我已经成功了:
var myMedia = new Media('/android_asset/www/media/face.mp3', mediaSuccess, mediaError);
这是我的整个js文件:
$("#mediapage").live("pagecreate", function(event) {
var page = this;
var mediaStatus;
mediaSuccess = function(){
}
mediaError = function(error) {
alert('Media Error: '+error.message);
}
var myMedia = new Media('/android_asset/www/media/face.mp3', mediaSuccess, mediaError);
$("#mediaplayLink").live("click",function(e) {
myMedia.play();
mediaStatus = setInterval(function() {
myMedia.getCurrentPosition(
function(position) {
if(position > -1) $("#status",page).html("Second "+position+" of "+myMedia.getDuration() + " seconds.");
})
}, 1000);
e.preventDefault();
});
$("#mediapauseLink").live("click",function(e) {
myMedia.pause();
e.preventDefault();
});
$("#mediastopLink").live("click",function(e) {
$("#status", page).html("");
clearInterval(mediaStatus);
myMedia.stop();
e.preventDefault();
});
});
和html代码:
<div data-role="content">
<div data-role="controlgroup" align="center">
<a id="mediaplayLink" data-inline="true"><img src='/android_asset/www/images/play.jpg'></a>
<a id="mediapauseLink" data-inline="true"><img src='/android_asset/www/images/pause.jpg'></a>
<a id="mediastopLink" data-inline="true"><img src='/android_asset/www/images/stop.jpg'></a>
</div>
<div id="status"></div>
</div>
请任何人帮忙??
答案 0 :(得分:-1)
你可以试试这段代码
public class Media_player extends ListActivity {
/** Called when the activity is first created. */
private static final String MEDIA_PATH = new String("/sdcard/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private int currentPosition = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
updateSongList();
}
private void updateSongList() {
// TODO Auto-generated method stub
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);
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
}
private void playSong(String string) {
// TODO Auto-generated method stub
try {
mp.reset();
mp.setDataSource(string);
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;
} else {
// Play next song
playSong(MEDIA_PATH + songs.get(currentPosition));
}
}
}
这个其他课程
public class Mp3Filter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return (filename.endsWith(".mp3"));
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:text="No songs found on SD Card." android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent" android:id="@+id/android:empty"></TextView>
<ListView android:layout_width="match_parent" android:drawSelectorOnTop="false" android:id="@+id/android:list" android:layout_height="286dp"></ListView>
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:background="@drawable/play" android:id="@+id/button1" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
song_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>