C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


■94820 / )  Re[1]: インデックスが配列の境界外です。が出てしまいました。
□投稿者/ エイ (8回)-(2020/05/21(Thu) 23:08:51)
コード

Imports libZPlay_player
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports libZPlay_player.libZPlay

Partial Public Class Form1
Dim a As String()
    Dim file As String()
    Dim IntSelectIndexPre As Integer = 0
    Private ReadOnly history1 As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
        ' If ListBox2.SelectedItem <> "" Then
        If ListBox2.SelectedItem IsNot "" Then
            TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem)
        Else
            Exit Sub
        End If

        If IntSelectIndexPre = ListBox2.SelectedIndex Then
            Exit Sub
        End If

        IntSelectIndexPre = ListBox2.SelectedIndex


        player.OpenFile(file(IntSelectIndexPre), TStreamFormat.sfAutodetect)


        showinfo()
        player.StartPlayback()
    End Sub
    Private Sub OpenFileDialog1_FileOk(sender As Object, e As ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        Dim files = OpenFileDialog1.FileNames
        Dim conflict = files.Where(AddressOf history1.Contains)

        If conflict.Any() Then
            e.Cancel = True
            Dim fileNames = conflict.Select(AddressOf System.IO.Path.GetFileName)
            MessageBox.Show("同じファイルがあります。" & vbCrLf & String.Join(vbCrLf, fileNames), "競合", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            Array.ForEach(files, AddressOf history1.Add)
        End If
        If LoadMode = 0 Then
            player.Close()
            If Not (player.OpenFile(OpenFileDialog1.FileName, TStreamFormat.sfAutodetect)) Then
                MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        ElseIf LoadMode = 1 Then
            player.Close()

            Dim format As TStreamFormat = player.GetFileFormat(OpenFileDialog1.FileName)

            Dim fInfo As New System.IO.FileInfo(OpenFileDialog1.FileName)
            Dim numBytes As Long = fInfo.Length
            Dim fStream As New System.IO.FileStream(OpenFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim br As New System.IO.BinaryReader(fStream)
            Dim stream_data() As Byte = Nothing

            stream_data = br.ReadBytes(CInt(Fix(numBytes)))
            If Not (player.OpenStream(True, False, stream_data, CUInt(numBytes), format)) Then
                MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            br.Close()
            fStream.Close()
        ElseIf LoadMode = 2 Then
            player.Close()

            Dim format As TStreamFormat = player.GetFileFormat(OpenFileDialog1.FileName)
            BufferCounter = 0

            Dim fInfo As New System.IO.FileInfo(OpenFileDialog1.FileName)
            Dim numBytes As UInteger = CUInt(fInfo.Length)
            If br IsNot Nothing Then
                br.Close()
            End If
            If fStream IsNot Nothing Then
                fStream.Close()
            End If

            br = Nothing
            fStream = Nothing

            fStream = New System.IO.FileStream(OpenFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            br = New System.IO.BinaryReader(fStream)
            Dim stream_data() As Byte = Nothing
            Dim small_chunk As UInteger = 0
            small_chunk = CType(Math.Min(100000, numBytes), UInteger)
            ' read small chunk of data
            stream_data = br.ReadBytes(CInt(Fix(small_chunk)))
            ' open stream
            If Not (player.OpenStream(True, True, stream_data, CUInt(stream_data.Length), format)) Then
                MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            ' read more data and push into stream
            stream_data = br.ReadBytes(CInt(Fix(small_chunk)))
            player.PushDataToStream(stream_data, CUInt(stream_data.Length))

        ElseIf LoadMode = 3 Then
            If Not (player.AddFile(OpenFileDialog1.FileName, TStreamFormat.sfAutodetect)) Then
                MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

        End If


        showinfo()
        player.StartPlayback()


    End Sub
    Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
        LoadMode = 3
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.Title = "開くファイルを指定"
        OpenFileDialog1.Filter = "All Supported Files|*.mp3;*.mp2;*.mp1;*.ogg;*.oga;*.flac;*.wav;*.ac3;*.aac|Mp3 Files|*.mp3|Mp2 Files|*.mp2|Mp1 Files|*.mp1|Ogg Files|*.ogg|FLAC files|*.flac|Wav files|*.wav|AC-3|*.ac3|AAC|*.aac"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.RestoreDirectory = True
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            file = OpenFileDialog1.SafeFileNames
            a = OpenFileDialog1.FileNames
            For b As Integer = 0 To file.Length - 1
                ListBox2.Items.Add(file(b))
                player.StartPlayback()
            Next
            ListBox2.SelectedIndex = 0
            TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem)

        End If
    End Sub

エラーが出る場所は
  player.OpenFile(file(IntSelectIndexPre), TStreamFormat.sfAutodetect)
ここで「インデックスが配列の境界外です。」と出ます。

コードが1万行以上あるのですいません。

インデックスが配列の境界外です。と出る場合、どのように修正したらいいんでしょうか?

元のコードをリストボックス追加して1曲目を開きました。
その次の2曲目を開き、2番目のリストボックスをクリックするとインデックスが配列の境界外です。と出ます。
お願い申し上げます。



サイトのダウンロードはDOWNLOAD: http://libzplay.sourceforge.net/download.html
 
libZPlay-2.02-SDK (2.12 MB)を使用しました。

libzplay.dllとlibZPlay.vbが必要です。




返信 編集キー/


管理者用

- Child Tree -