无法让 VIdeoView 进行随机播放
我在 android 中有一个视频视图,它正在从文本文件中读取行并播放它们。该文本文件包含指向网络服务器上的音频和视频文件的链接。然而,当它到达歌曲/视频的结尾时,它不会播放下一首。
看来我需要实现一个 OnCompletionListener 但我不知道该怎么做。这是正确的下降路径吗?
我尝试将其放入循环中执行播放方法 X 次,但它只是挂起。我假设它只是不断随机洗牌,直到到达循环末尾。
我还尝试使用 videoVIew.getDuration() 让它暂停音频/视频轨道的长度,
代码如下:
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
LineNumberReader rdr = new LineNumberReader(br);
int min = 1; //Line 1 (start of file)
int max = 20; // Line 20 (end of file)
Random r = new Random();
int someRandomNo = r.nextInt(max - min + 1) + min;
textView1.setText(Integer.toString(someRandomNo));
// get random number - go to line - save it
rdr.setLineNumber(someRandomNo);
int linenum = 0;
String theLine = "";
while (linenum < someRandomNo) {
theLine = br.readLine();
linenum++;
}
textView1.setText(theLine);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set Audio/Video to play
Uri video = Uri.parse(theLine);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
catch (Exception e) {
}
I have a videoview in android that is reading lines from a text file and playing them. The text file contains links to audio and video files on a webserver. However when it reaches the end of the song/video it will not play the next one.
It seems that I need to implement an OnCompletionListener
but I have no idea how to do that. Would that be the correct path to go down?
I have tried putting it in a loop to perform the play method X amount of times but it just hangs. I am assuming it just keeps shuffling randomly until it reaches the end of the loop.
I have also attempted to get it to pause for the length of the audio/video track by using videoVIew.getDuration()
The code is below:
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
LineNumberReader rdr = new LineNumberReader(br);
int min = 1; //Line 1 (start of file)
int max = 20; // Line 20 (end of file)
Random r = new Random();
int someRandomNo = r.nextInt(max - min + 1) + min;
textView1.setText(Integer.toString(someRandomNo));
// get random number - go to line - save it
rdr.setLineNumber(someRandomNo);
int linenum = 0;
String theLine = "";
while (linenum < someRandomNo) {
theLine = br.readLine();
linenum++;
}
textView1.setText(theLine);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
// Set Audio/Video to play
Uri video = Uri.parse(theLine);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
}
catch (Exception e) {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你想要的:
以及 android dev 上的 onCompletionListener 资源 - http:// /developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
就其价值而言,我建议您对文件执行一次 IO,并将 Line 保存在数组中或ArrayList,这样您就可以访问它,甚至可以创建一个自定义类来处理洗牌。例如,您可以让它跟踪已发送的字符串,即已播放的视频,然后随机选择一个尚未播放的新视频。
This is what you want:
And the onCompletionListener resource on android dev- http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
And for what its worth, I would suggest you do your IO with the file once, and save theLine in an array or ArrayList and that way you can access it, or even make a custom class to handle shuffling. For example you could have it keep track of which strings had been sent, ie which videos has been played, and then randomly choose a new one that had not been played yet.
AJcodez 的答案正是我所需要的,我只是不确定如何使用 OnCompletion Listener。
我将 OnCreate 代码放入其自己的方法中,然后让侦听器每次都调用它。我也没有为该类实现 MediaPayer.OnCompletition 监听器。
我遇到的另一个 SO 链接
AJcodez answer was what I needed, I was just unsure how to use the OnCompletion Listener.
I put my OnCreate code into its own method and then simply had the listener call it everytime. I also wasn't implementing the
MediaPayer.OnCompletition
Listener for the class.Another SO link that I came across