>在原始文件中设置了mp3
>读取它们并将它们设置为数组
>获取数组大小
>生成一个随机数并将其作为元素传递给媒体播放器
>所以玩家跑
> mp3剪辑不到2秒所以我等待2秒并播放下一个剪辑和一种循环,这需要运行直到我停止
这是个主意
这是代码
包com.hello.musicplayer;
import java.lang.reflect.FIEld;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Timer;import java.util.TimerTask;import androID.app.Activity;import androID.media.MediaPlayer;import androID.os.Bundle;import androID.os.Handler;import androID.vIEw.VIEw;import androID.Widget.Seekbar;import androID.Widget.TextVIEw;public class AndroIDMediaPlayer extends Activity { // two private MediaPlayer mediaPlayer; public TextVIEw songname, duration; private double timeElapsed = 0, finalTime = 0; private int forwardTime = 2000, backwardTime = 2000; private Handler durationHandler = new Handler(); //private Seekbar seekbar; private FIEld[] fIElds; private String name; private int resourceID; private List<String> songnames; //private List<String> songsdisplay; private int nameIntRandom; private double timeRemaining; private int value; private int countinitilaze; private int pauseChecker=0; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); songnames = new ArrayList(); // songsdisplay =new ArrayList(); ListRaw(); initializeVIEws(); mediaPlayer.setonErrorListener(new MediaPlayer.OnErrorListener() { @OverrIDe public boolean one rror(MediaPlayer mp, int what, int extra) { System.out.println("mediaPlayer error"); initializeVIEws(); return true; } });// seekbar.setonSeekbarchangelistener(new Seekbar.OnSeekbarchangelistener() {// @OverrIDe// public voID onProgressChanged(Seekbar seekbar, int progress, boolean fromUser) {//// System.out.println("progress" + (progress));// System.out.println("progress final - " + finalTime);////// }//// @OverrIDe// public voID onStartTrackingtouch(Seekbar seekbar) {//// }//// @OverrIDe// public voID onStopTrackingtouch(Seekbar seekbar) {//// }// }); } private voID ListRaw() { System.out.println("ListRaw ListRaw"); fIElds = R.raw.class.getFIElds(); for (int count = 0; count < fIElds.length; count++) { System.out.println("ListRaw names.... " + fIElds[count].getname()); try { // String songsstringnames =fIElds[count].getname(); //songsdisplay.add(songsstringnames); resourceID = fIElds[count].getInt(fIElds[count]); System.out.println("ListRaw resourceIDresourceID " + resourceID); name = String.valueOf(resourceID); songnames.add(name); System.out.println("ListRaw songnames.size();" +songnames.size()); songnames.size(); } catch (illegalaccessexception e) { e.printstacktrace(); System.out.println("catch" + e); } } } public voID initializeVIEws(){ Random randomGenerator = new Random(); value = randomGenerator.nextInt((songnames.size())); System.out.println("AndroIDMediaPlayer value -" + value); songname = (TextVIEw) findVIEwByID(R.ID.songname); System.out.println("AndroIDMediaPlayer songnames array-" + songnames); nameIntRandom = Integer.parseInt(songnames.get(value)); mediaPlayer = MediaPlayer.create(this, nameIntRandom); //finalTime = mediaPlayer.getDuration(); //duration = (TextVIEw) findVIEwByID(R.ID.songDuration); //seekbar = (Seekbar) findVIEwByID(R.ID.seekbar); //songname.setText(fIElds[value].getname()); //seekbar.setMax((int) finalTime); //seekbar.setClickable(false); if (countinitilaze >0) { if (finalTime <2000){ play(); } else { playingBigClip(); System.out.println("playing a big clip "); } } countinitilaze++; } /////// public voID play(){if (pauseChecker == 0 ){ System.out.println("AndroIDMediaPlayer play ");// timeElapsed = mediaPlayer.getCurrentposition();// seekbar.setProgress((int) timeElapsed);try { mediaPlayer.start(); if(!mediaPlayer.isPlaying()){ initializeVIEws(); }}catch (Exception e){ initializeVIEws();} int delay = 1000;// in ms Timer timer = new Timer(); timer.schedule(new TimerTask() { public voID run() { // mediaPlayer.stop(); initializeVIEws(); } }, delay);}// if((timeRemaining/10000) < 1000 ){// System.out.println("progress fully compleated ");// mediaPlayer.stop();// initializeVIEws();//// } } ///////////////////////////////////// play mp3 song public voID play(VIEw vIEw) { pauseChecker = 0; System.out.println("AndroIDMediaPlayer play onclick"); if (finalTime <2000){ play(); } else { playingBigClip(); } //durationHandler.postDelayed(updateSeekbarTime, 10); } public voID playingBigClip(){ System.out.println("play playingBigClip()"); //mediaPlayer = MediaPlayer.create(this, nameIntRandom); mediaPlayer.start(); //timeElapsed = mediaPlayer.getCurrentposition(); //seekbar.setProgress((int) timeElapsed); if((timeRemaining/1000) < 1){ System.out.println("big clip time remains "); initializeVIEws(); } } public voID pause(VIEw vIEw) {pauseChecker = 1; System.out.println("AndroIDMediaPlayer pause onclick"); mediaPlayer.pause(); }}
mp3剪辑工作正常
当它在25秒内运行时它会停止
有时它会给出这个例外
java.util.concurrent.TimeoutException: androID.media.MediaPlayer.finalize() timed out after 10 seconds at androID.media.MediaPlayer.native_finalize(Native Method) at androID.media.MediaPlayer.finalize(MediaPlayer.java:3284) at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:194) at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:177) at java.lang.Thread.run(Thread.java:818)01-26 08:15:56.410 12699-12720/? E/DBG_POliCYDM﹕ [com.policydm.XDMbroadcastReceiver(108/run)] network is unserviceable
我需要继续玩媒体播放器
任何帮助?
XML <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_gravity="center" androID:background="#333333" androID:orIEntation="vertical" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" > <TextVIEw androID:ID="@+ID/songname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="songname" /> <ImageVIEw androID:ID="@+ID/mp3Image" androID:layout_wIDth="match_parent" androID:layout_height="200dp" androID:padding="30dp" androID:src="@drawable/music" androID:background="#ffffff" androID:layout_margin="30dp" /> <TextVIEw androID:ID="@+ID/songDuration" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:text="songDuration" /> <Seekbar androID:ID="@+ID/seekbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_margintop="30dp" androID:gravity="center_horizontal" androID:orIEntation="horizontal" > <Imagebutton androID:ID="@+ID/media_rew" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="14dp" androID:onClick="rewind" androID:src="@androID:drawable/ic_media_rew" /> <Imagebutton androID:ID="@+ID/media_pause" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="14dp" androID:onClick="pause" androID:src="@androID:drawable/ic_media_pause" /> <Imagebutton androID:ID="@+ID/media_play" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="14dp" androID:onClick="play" androID:src="@androID:drawable/ic_media_play" /> <Imagebutton androID:ID="@+ID/media_ff" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="14dp" androID:onClick="forward" androID:src="@androID:drawable/ic_media_ff" /> </linearLayout></linearLayout>
解决方法:
当您按原样重复创建MediaPlayer实例时,您需要在创建下一个播放器之前调用前一个播放器上的release().引用MediaPlayer
docs:
It is also recommended that once a MediaPlayer object is no longer being used, call
release()
immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as harDWare acceleration components and failure to callrelease()
may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.
虽然您使用单个变量来引用每个新玩家,但旧实例仍然“闲置”一段时间,并最终由于未明确释放而导致声明的异常.
总结以上是内存溢出为你收集整理的Android媒体播放器异常在10秒后超时全部内容,希望文章能够帮你解决Android媒体播放器异常在10秒后超时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)