mono for android 获取不到正确mediaPlayer的duration 并且Stop()方法和Pause()方法效果居然一样

美人迟暮了又迟暮 2016-02-03 05:03:30
using System;
using System.Threading;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
using Android.Util;

namespace mediaPlayer
{
[Activity(Label = "mediaPlayer", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity,MediaPlayer.IOnCompletionListener
{
int count = 1;
MediaPlayer idPlayer;
TextView tv;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);

button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

Button playId = FindViewById<Button>(Resource.Id.button1);
playId.Click += playId_Click;
Button stop = FindViewById<Button>(Resource.Id.button2);
stop.Click += playNet_Click;
Button pause = FindViewById<Button>(Resource.Id.button3);
pause.Click += playFile_Click;
Button video = FindViewById<Button>(Resource.Id.button4);
video.Click += video_Click;
tv = FindViewById<TextView>(Resource.Id.textView1);
}

void video_Click(object sender, EventArgs e)
{
Intent intent = new Intent(this, typeof(Video));
StartActivity(intent);
}


void playFile_Click(object sender, EventArgs e)
{
idPlayer.Pause();
}

void playNet_Click(object sender, EventArgs e)
{
idPlayer.Stop();
//int position = idPlayer.CurrentPosition / 1000;
//Toast.MakeText(this, position.ToString(), ToastLength.Short).Show();
}

void playId_Click(object sender, EventArgs e)
{
idPlayer = new MediaPlayer();
if (idPlayer != null)
{
idPlayer.SetDataSource(this.Resources.OpenRawResourceFd(Resource.Raw.yshq).FileDescriptor);
idPlayer.SetOnCompletionListener(this);
idPlayer.Prepare();
idPlayer.Start();
Thread thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}

private void Run()
{
while (true)
{
Thread.Sleep(1000);
int i = (idPlayer.CurrentPosition / 1000);
RunOnUiThread(() =>
{
//打印当前音乐播放当前时间和总时间
//当前时间输出正确
//总共时间输出415 (总共时间为15s)
tv.Text = i.ToString() + "&&&" +(idPlayer.Duration).ToString();
});
if(i == 15)
{
break;
}
}
}

public void OnCompletion(MediaPlayer mp)
{
mp.Stop();
mp.Release();
}
}
}
...全文
333 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xulichun9 2016-04-13
  • 打赏
  • 举报
回复
楼主,我做了一下测试是可以的啊,以下是我的代码,我刚开始接触mono for android的。以后多多交流 namespace App1 { [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { int count = 0; MediaPlayer mp = new MediaPlayer(); protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById<Button>(Resource.Id.MyButton); SurfaceView sfv = FindViewById<SurfaceView>(Resource.Id.surfaceView1); button.Click += delegate { button.Text = string.Format("{0} clicks!", ++count); }; button.Click += (sender, e) => { //播放视频代码 sfv.Click += Sfv_Click; mp.SetDisplay(sfv.Holder); mp.SetDataSource("/storage/external_storage/sdcard1/111.mp4"); mp.Prepare(); mp.Start(); }; } private void Sfv_Click(object sender, EventArgs e) { if (mp.IsPlaying) { mp.Pause(); } else { mp.Start(); } } } }
Justin-Liu 2016-02-05
  • 打赏
  • 举报
回复
有没有跟Pause对应的Resume什么的方法? Play可能就是重新播放的方法
Justin-Liu 2016-02-04
  • 打赏
  • 举报
回复
Pause之后也不能播放了?
  • 打赏
  • 举报
回复
Pause和Stop之后再调用Start 又重新播放

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧