Visual studio 2015 Xamarin音频播放器进度条不一致
我对Xamarin Android中的进度条有问题。我使用下面的代码播放特定的音频文件Visual studio 2015 Xamarin音频播放器进度条不一致,visual-studio-2015,xamarin.android,xamarin.forms,Visual Studio 2015,Xamarin.android,Xamarin.forms,我对Xamarin Android中的进度条有问题。我使用下面的代码播放特定的音频文件 await CrossMediaManager.Current.Play(await DependencyService.Get<IMedia>().GetPath(answer.ResourceURL)); 我把这个进度条码放在了游戏之后。 所以现在我的问题是,在我的屏幕上有一些音频文件,比如5个播放按钮。 如果我点击第二个文件,音频播放得很完美。但是第5个文件的进度条也在移动,但音频没有播放
await CrossMediaManager.Current.Play(await DependencyService.Get<IMedia>().GetPath(answer.ResourceURL));
我把这个进度条码放在了游戏之后。
所以现在我的问题是,在我的屏幕上有一些音频文件,比如5个播放按钮。
如果我点击第二个文件,音频播放得很完美。但是第5个文件的进度条也在移动,但音频没有播放。只移动进度条。
请找到附件中的图片
如果我在构造函数中放置进度条代码,则所有文件的所有进度条都将被移动,但只有一个文件正在播放
有人请帮我解决这个问题
我使用的是card组件,它将根据ViewModel中DB中的文件数进行迭代。文件的计数是动态的 我不确定您使用的是哪个控件,但根据您的描述,您可能正在使用
ListView
或RecyclerView
或类似的控件,其项可以使用适配器自动插入列表
然后,对于像这样的控件,不建议使用其名称来检索项中控件的实例,因为每个项都有一个同名的控件,特别是在使用Xamarin.Forms进行开发时,如果您对ItemTemplate
使用in-PCL和DataTemplate
,它就像每个项目的一个结构
假设您在PCL中使用的是ListView
,而不是在Android平台中,那么您可以使用这样的示例代码
首先创建Image
的子类,这样我们就可以为每个项目提供一个ID,例如:
public class MyImage : Image
{
public int ItemID
{
get { return (int)GetValue(ItemIDProperty); }
set { SetValue(ItemIDProperty, value); }
}
public static readonly BindableProperty ItemIDProperty = BindableProperty.Create<MyImage, int>(p => p.ItemID, default(int));
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YOURNAMESPACE"
x:Class="YOURNAMESPACE.MainPage">
<ContentPage.Content>
<ListView x:Name="lv">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill">
<local:MyImage x:Name="xplay" Source="play_circle.png" ItemID="{Binding id}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TrackEntity3" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</local:MyImage>
<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label x:Name="elapseTime" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding elapsetime,Mode=OneWay}"></Label>
<Label x:Name="totalTime" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="End" Text="{Binding totaltime,Mode=OneWay}"></Label>
</StackLayout>
<ProgressBar x:Name="progressBar" WidthRequest="300" Progress="{Binding progress,Mode=OneWay}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
在TrackEntity3
事件中:
private MyMedia currentItem;
private void TrackEntity3(object sender, EventArgs e)
{
var image = sender as MyImage;
var id = image.ItemID;
foreach (var source in lv.ItemsSource)
{
var item = source as MyMedia;
if (item.id == id)
{
//play music here
//find labels and progressbar
if (currentItem != null)
{
currentItem.progress = 0;
currentItem.totaltime = "";
currentItem.elapsetime = "";
}
currentItem = item;
}
}
}
在CrossMediaManager.Current.PlayingChanged
事件中,直接如下更改项目:
CrossMediaManager.Current.PlayingChanged += (sender, ev) =>
{
currentItem.progress = ...;
currentItem.elapsetime = ...;
currentItem.totaltime = ...;
};
由于它是一个列表视图
,因此您可以使用可观测集合添加项目
:
private ObservableCollection<MyMedia> collection = new ObservableCollection<MyMedia>();
public MainPage()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
collection.Add(new MyMedia { id = i });
}
lv.ItemsSource = collection;
}
private observetecollection collection=new observetecollection();
公共主页()
{
初始化组件();
对于(int i=0;i<5;i++)
{
添加(新的MyMedia{id=i});
}
lv.ItemsSource=集合;
}
听起来像是在使用MVVM模式,您可以将代码移到viewmodel后面。如果您正在使用其他控件动态添加项,则可以留下注释
我使用的是card组件,它将根据ViewModel中DB中的文件数进行迭代。文件的计数是动态的
我不确定您使用的是哪个控件,但根据您的描述,您可能正在使用ListView
或RecyclerView
或类似的控件,其项可以使用适配器自动插入列表
然后,对于像这样的控件,不建议使用其名称来检索项中控件的实例,因为每个项都有一个同名的控件,特别是在使用Xamarin.Forms进行开发时,如果您对ItemTemplate
使用in-PCL和DataTemplate
,它就像每个项目的一个结构
假设您在PCL中使用的是ListView
,而不是在Android平台中,那么您可以使用这样的示例代码
首先创建Image
的子类,这样我们就可以为每个项目提供一个ID,例如:
public class MyImage : Image
{
public int ItemID
{
get { return (int)GetValue(ItemIDProperty); }
set { SetValue(ItemIDProperty, value); }
}
public static readonly BindableProperty ItemIDProperty = BindableProperty.Create<MyImage, int>(p => p.ItemID, default(int));
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YOURNAMESPACE"
x:Class="YOURNAMESPACE.MainPage">
<ContentPage.Content>
<ListView x:Name="lv">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill">
<local:MyImage x:Name="xplay" Source="play_circle.png" ItemID="{Binding id}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TrackEntity3" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</local:MyImage>
<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label x:Name="elapseTime" HorizontalOptions="Start" HorizontalTextAlignment="Start" Text="{Binding elapsetime,Mode=OneWay}"></Label>
<Label x:Name="totalTime" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="End" Text="{Binding totaltime,Mode=OneWay}"></Label>
</StackLayout>
<ProgressBar x:Name="progressBar" WidthRequest="300" Progress="{Binding progress,Mode=OneWay}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
在TrackEntity3
事件中:
private MyMedia currentItem;
private void TrackEntity3(object sender, EventArgs e)
{
var image = sender as MyImage;
var id = image.ItemID;
foreach (var source in lv.ItemsSource)
{
var item = source as MyMedia;
if (item.id == id)
{
//play music here
//find labels and progressbar
if (currentItem != null)
{
currentItem.progress = 0;
currentItem.totaltime = "";
currentItem.elapsetime = "";
}
currentItem = item;
}
}
}
在CrossMediaManager.Current.PlayingChanged
事件中,直接如下更改项目:
CrossMediaManager.Current.PlayingChanged += (sender, ev) =>
{
currentItem.progress = ...;
currentItem.elapsetime = ...;
currentItem.totaltime = ...;
};
由于它是一个列表视图
,因此您可以使用可观测集合添加项目
:
private ObservableCollection<MyMedia> collection = new ObservableCollection<MyMedia>();
public MainPage()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
collection.Add(new MyMedia { id = i });
}
lv.ItemsSource = collection;
}
private observetecollection collection=new observetecollection();
公共主页()
{
初始化组件();
对于(int i=0;i<5;i++)
{
添加(新的MyMedia{id=i});
}
lv.ItemsSource=集合;
}
听起来像是在使用MVVM模式,您可以将代码移到viewmodel后面。如果您正在使用其他控件动态添加项目,您可以留下评论。能否显示5个播放按钮和5个进度条的布局?
我正在使用卡片组件,它将根据my ViewModel中数据库中的文件数进行迭代。文件的计数是动态的@Grace Fengo能否显示5个播放按钮和5个进度条的布局?
我正在使用卡片组件,该组件将根据我的ViewModel中数据库中的文件数进行迭代。文件的计数是动态的@感谢您的快速回复。在某种程度上,我只是在浏览ListView。请找到下面的代码:
下面是我的设计组件//播放按钮和进度条的设计在这里
@Navya,是的,你用了你的卡:PlayersComponent
,没关系,我只是提供了一个示例代码,由于您的点击事件已附加到映像,因此,使用映像本身查找相关项,并使用数据绑定和INotifyPropertyChanged
通知数据源是否已更改非常重要。感谢您的快速响应。在某种程度上,我仅在ListView中进行迭代。请找到下面的代码:
下面是我的设计组件//播放按钮和进度条的设计在这里
@Navya,是的,你用了你的卡:PlayersComponent
,没关系,我只是提供了一个示例代码,自从你打了个平手